import Login from '@/views/Login.vue'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Register from '@/views/Register.vue'
import Detail from '@/views/Detail.vue'
import Home from '@/views/Home.vue'
import Article from '@/views/Layout/Article.vue'
import Collect from '@/views/Layout/Collect.vue'
import Like from '@/views/Layout/Like.vue'
import User from '@/views/Layout/User.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/detail', component: Detail },
  {
    path: '/',
    redirect: '/article',
    component: Home,
    children: [
      { path: 'article', component: Article },
      { path: 'collect', component: Collect },
      { path: 'like', component: Like },
      { path: 'user', component: User }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 必须在创建router之后，导出之前，加入全局前置导航守卫
router.beforeEach((to, from, next) => {
  // console.log(to) // to.path 表示跳转的路由地址
  // next()
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    // 访问面经列表页、详情页、收藏页......，还需要判断是否有token
    if (localStorage.getItem('mobile-token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
